package com.k1998.mybatis.service.impl;

import com.alibaba.fastjson.JSON;
import com.k1998.mybatis.bean.Message;
import com.k1998.mybatis.dao.IMessageDao;
import com.k1998.mybatis.service.MessageService;
import com.k1998.mybatis.utils.BatchInfo;
import com.k1998.mybatis.utils.JxUtils;
import com.k1998.mybatis.utils.QueryInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
@Service("MessageImpl")
public class MessageImpl implements MessageService {
    @Autowired
    private IMessageDao iMessageDao;
    @Override
    public String detailMessage(Message message) {
        Message message1 = iMessageDao.detailMessage(message);
        HashMap<String, Object> res = new HashMap<>();
        res.put("data",message1);
        res.put("code","success");
        String message_json = JSON.toJSONString(res);
        return message_json;
    }

    @Override
    public String insertMessage(Message message) {
        int i = iMessageDao.insertMessage(message);
        String str = i >0?"success":"error";
        return str;
    }

    @Override
    public String updateMessageById(Message message) {
        int i = iMessageDao.updateMessageById(message);
        String str = i >0?"success":"error";
        return str;
    }

    @Override
    public int batchDeleteMessage(BatchInfo info) {
        List<String> list= JxUtils.stringToList(info.getIds());
        if(null==list){return 0;}
        int i = iMessageDao.batchDeleteMessage(list);
        return i;
    }

    @Override
    public String findMessageByDisplay(QueryInfo queryInfo) {
        if(null==queryInfo.getQuery()){queryInfo.setQuery("");}
        if(queryInfo.getDisplay() >1 || queryInfo.getDisplay() < -1) {queryInfo.setDisplay(-1);}
        int numbers = iMessageDao.getMessageCountsByDisplay("%"+queryInfo.getQuery()+"%", queryInfo.getDisplay());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Message> messages = iMessageDao.findMessageByDisplay("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize(),queryInfo.getDisplay());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",messages);
        res.put("code","success");
        String message_json = JSON.toJSONString(res);
        return message_json;
    }

    @Override
    public String reversalMessageDisplay(BatchInfo batchInfo) {
        List<String> list= JxUtils.stringToList(batchInfo.getIds());
        int i = iMessageDao.reversalMessageDisplay(list);
        String str = i==list.size()?"success":"error";
        return str;
    }
}
